package com.guoxue.action.back;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.guoxue.action.front.BaseAction;
import com.guoxue.domain.*;
import com.guoxue.service.IBookConfigService;
import com.guoxue.service.IChapterConfigService;
import com.guoxue.service.IDistrictService;
import com.guoxue.service.IExpertReadingService;
import com.guoxue.service.IExplainConfigService;
import com.guoxue.service.IPlanConfigService;
import com.guoxue.service.IPropertyConfigService;
import com.guoxue.service.IPublishLogsService;
import com.guoxue.service.IRecordConfigService;
import com.guoxue.service.IStageConfigService;
import com.guoxue.service.IStoryConfigService;
import com.guoxue.util.LoggerUtil;
/**
 * 处理上传xls后修改及删除功能实现Action
 * @author young
 *
 */
@Controller
@RequestMapping(value="Config")
public class ConfigAction extends BaseAction{

	@Resource
	private IRecordConfigService recordServcie;
	@Resource
	private IBookConfigService bookServcie;
	@Resource
	private IChapterConfigService chapterService;
	@Resource
	private IExpertReadingService expertService;
	@Resource
	private IExplainConfigService explainService;
	@Resource
	private IPlanConfigService planConfigService;
	@Resource
	private IPropertyConfigService propertyConfigService;
	@Resource
	private IDistrictService districtService;
	@Resource
	private IStageConfigService stageConfigService;
	@Resource
	private IStoryConfigService storyService;
	@Resource
	private IPublishLogsService publishService;
	/**
	 *  章节配置修改
	 * @param req
	 * @param resp
	 * @param cc
	 * @return
	 */
	@RequestMapping("chapter")
	@ResponseBody
	public JSON  updateConfigs(HttpServletRequest req, HttpServletResponse resp, ChapterConfigVo cc){
		try {
			if (cc.getChapterId()<=0){
				return Message(ERROR, "章节编号不能为空", null);
			} else {
				chapterService.saveChapterConfig(cc);
				return Message(SUCCESS, "章节目录修改成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据保存失败");
		}
	}
	/**
	 *  点击配置修改
	 * @param req
	 * @param resp
	 * @param bc
	 * @return
	 */
	@RequestMapping("book")
	@ResponseBody
	public JSON  updateConfigs(HttpServletRequest req, HttpServletResponse resp, BookConfigVo bc){
		try {
			if (bc.getBookId()<=0){
				return Message(ERROR, "典籍编号不能为空", null);
			} else {
				bookServcie.saveBookConfig(bc);
				return Message(SUCCESS, "典籍配置修改成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据保存失败");
		}
	}
	/**
	 * 关卡配置修改
	 * @param req
	 * @param resp
	 * @param sc
	 * @return
	 */
	@RequestMapping("level")
	@ResponseBody
	public JSON  updateConfigs(HttpServletRequest req, HttpServletResponse resp, StageConfigVo sc){
		try {
			if (sc.getStageId()<=0){
				return Message(ERROR, "关卡ID不能为空", null);
			} else {
				stageConfigService.saveStageConfig(sc);
				return Message(SUCCESS, "关卡配置修改成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据保存失败");
		}
	}
	/**
	 *  专家吟诵修改
	 * @param req
	 * @param resp
	 * @param er
	 * @return
	 */
	@RequestMapping("master")
	@ResponseBody
	public JSON  updateConfigs(HttpServletRequest req, HttpServletResponse resp, ExpertReadingVo er){
		try {
			if (er.getWorkId()<=0){
				return Message(ERROR, "作品ID不能为空", null);
			} else {
				expertService.saveExpertReading(er);
				return Message(SUCCESS, "专家吟诵修改成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据保存失败");
		}
	}
	/**
	 *  释义修改
	 * @param req
	 * @param resp
	 * @param ec
	 * @return
	 */
	@RequestMapping("mean")
	@ResponseBody
	public JSON  updateConfigs(HttpServletRequest req, HttpServletResponse resp, ExplainConfigVo ec){
		try {
			if (ec.getExplainId()<=0){
				return Message(ERROR, "作品ID不能为空", null);
			} else {
				explainService.saveExplain(ec);
				return Message(SUCCESS, "释义修改成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据保存失败");
		}
	}
	@RequestMapping("plan")
	@ResponseBody
	public JSON  updateConfigs(HttpServletRequest req, HttpServletResponse resp, PlanConfigVo pc){
		try {
			if (pc.getPlanId()<=0){
				return Message(ERROR, "作品ID不能为空", null);
			} else {
				planConfigService.savePlanConfig(pc);
				return Message(SUCCESS, "计划修改成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据保存失败");
		}
	}
	@RequestMapping("prop")
	@ResponseBody
	public JSON  updateConfigs(HttpServletRequest req, HttpServletResponse resp, PropertyConfigVo pc){
		try {
			if (pc.getPropId()<=0){
				return Message(ERROR, "商品ID不能为空", null);
			} else {
				propertyConfigService.savePropertyConfig(pc);
				return Message(SUCCESS, "商品配置修改成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据保存失败");
		}
	}
	@RequestMapping("region")
	@ResponseBody
	public JSON  updateConfigs(HttpServletRequest req, HttpServletResponse resp, DistrictVo d){
		try {
			if (d.getId()<=0){
				return Message(ERROR, "位置区域ID", null);
			} else {
				d.setId(0);
				districtService.saveDistrict(d);
				return Message(SUCCESS, "区域配置修改成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据保存失败");
		}
	}
	@RequestMapping("story")
	@ResponseBody
	public JSON  updateConfigs(HttpServletRequest req, HttpServletResponse resp, StoryConfigVo sc){
		try {
			if (sc.getStoryId()<=0){
				return Message(ERROR, "故事ID不能为空", null);
			} else {
				storyService.saveStoryConfig(sc);
				return Message(SUCCESS, "故事配置修改成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据保存失败");
		}
	}
	@RequestMapping("tapes")
	@ResponseBody
	public JSON  updateConfigs(HttpServletRequest req, HttpServletResponse resp, RecordConfigVo rc){
		try {
			if (rc.getWordId()<=0){
				return Message(ERROR, "作品ID不能为空", null);
			} else {
				recordServcie.saveRecordConfig(rc);
				return Message(SUCCESS, "释义修改成功", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据保存失败");
		}
	}
	/**
	 *  对应配置删除方法
	 * @param req
	 * @param resp
	 * @param type
	 * @param id
	 * @return
	 */
	@RequestMapping("del/{type}")
	@ResponseBody
	public JSON delConfig(HttpServletRequest req, HttpServletResponse resp, @PathVariable String type, Integer id){
		try {
			if (type.equals("chapter")) {
				chapterService.deleteCapter(id);
				return Message(SUCCESS, "删除成功", null);
			} else if(type.equals("book")){
				bookServcie.deleteBook(id);
				return Message(SUCCESS, "删除成功", null);
			} else if (type.equals("level")){
				stageConfigService.deleteStage(id);
				return Message(SUCCESS, "删除成功", null);
			} else if (type.equals("master")){
				expertService.deleteExpertConfig(id);
				return Message(SUCCESS, "删除成功", null);
			} else if (type.equals("mean")){
				explainService.deleteExplainConfig(id);
				return Message(SUCCESS, "删除成功", null);
			} else if (type.equals("plan")) {
				planConfigService.deletePlanConfig(id);
				return Message(SUCCESS, "删除成功", null);
			} else if (type.equals("prop")){
				propertyConfigService.deleteProperty(id);
				return Message(SUCCESS, "删除成功", null);
			} else if (type.equals("region")){
				districtService.deleteDistrict(id);
				return Message(SUCCESS, "删除成功", null);
			} else if(type.equals("story")){
				storyService.deleteStory(id);
				return Message(SUCCESS, "删除成功", null);
			} else if (type.equals("tapes")){
				recordServcie.deleteRecordConfig(id);
				return Message(SUCCESS, "删除成功", null);
			} else if (type.equals("publish")) {
				publishService.deleteForStatus(id);
				return Message(SUCCESS, "删除成功", null);
			} else {
				return Message(ERROR, "删除失败，未找到匹配类型", null);
			}
		} catch (Exception e) {
			return LoggerUtil.logInfo(e,"数据操作失败");
		}
	}
}
